package com.bj58.mis.apm.server.logreceiver.service.impl;

import com.bj58.mis.apm.server.logreceiver.dto.JVMModel;
import com.bj58.mis.apm.server.logreceiver.model.JvmGcInfo;
import com.bj58.mis.apm.server.logreceiver.model.JvmInfo;
import com.bj58.mis.apm.server.logreceiver.model.JvmMemoryPoolInfo;
import com.bj58.mis.apm.server.logreceiver.repository.JvmLogRepository;
import com.bj58.mis.apm.server.logreceiver.service.JvmLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.Map;

@Service
@Transactional
public class JvmLogServiceImpl implements JvmLogService {
    @Autowired
    private JvmLogRepository jvmLogRepository;

    @Override
    public void receive(JVMModel model) {
        JvmInfo jvmInfo = new JvmInfo();
        jvmInfo.setAppName(model.getAppName());
        jvmInfo.setHostName(model.getHostName());
        jvmInfo.setCollectTime(new Timestamp(model.getCollectTime()));
        jvmInfo.setVersion(model.getVersion());
        jvmInfo.setVendor(model.getVendor());
        jvmInfo.setJavaHome(model.getJavahome());

        //类加载情况
        jvmInfo.setLoadedClassCount(model.getClassLoadingInfo().get("LoadedClassCount").intValue());
        jvmInfo.setUnloadedClassCount(model.getClassLoadingInfo().get("UnloadedClassCount").intValue());
        jvmInfo.setTotalLoadedClassCount(model.getClassLoadingInfo().get("TotalLoadedClassCount").intValue());

        //线程情况
        jvmInfo.setCurrentThreadCount(model.getThreadInfo().get("CurrentThreadCount").intValue());
        jvmInfo.setTotalStartedThreadCount(model.getThreadInfo().get("TotalStartedThreadCount").intValue());
        jvmInfo.setPeakThreadCount(model.getThreadInfo().get("PeakThreadCount").intValue());
        jvmInfo.setDaemonThreadCount(model.getThreadInfo().get("DaemonThreadCount").intValue());

        //堆内存情况
        jvmInfo.setHeapInit(model.getHeap().get("init"));
        jvmInfo.setHeapCommitted(model.getHeap().get("committed"));
        jvmInfo.setHeapMax(model.getHeap().get("max"));
        jvmInfo.setHeapUsed(model.getHeap().get("used"));

        //非堆内存情况
        jvmInfo.setNonHeapInit(model.getNonHeap().get("init"));
        jvmInfo.setNonHeapCommitted(model.getNonHeap().get("committed"));
        jvmInfo.setNonHeapMax(model.getNonHeap().get("max"));
        jvmInfo.setNonHeapUsed(model.getNonHeap().get("used"));

        //gc情况
        for(Map<String,String> map : model.getGc()){
            JvmGcInfo gcinfo=new JvmGcInfo();
            gcinfo.setName(map.get("name"));
            gcinfo.setGCTime(Long.parseLong(map.get("GCTime")));
            gcinfo.setGCCount(Long.parseLong(map.get("Count")));
            jvmInfo.getJvmGcInfo().add(gcinfo);
        }

        //内存池情况
        for(Map<String,String> map : model.getMemoryPool()){
            JvmMemoryPoolInfo poolinfo=new JvmMemoryPoolInfo();
            poolinfo.setName(map.get("name"));
            poolinfo.setMempoolInit(Long.parseLong(map.get("init")));
            poolinfo.setMempoolCommitted(Long.parseLong(map.get("committed")));
            poolinfo.setMempoolMax(Long.parseLong(map.get("max")));
            poolinfo.setMempoolUsed(Long.parseLong(map.get("used")));
            jvmInfo.getJvmMemoryPoolInfo().add(poolinfo);
        }

        jvmLogRepository.save(jvmInfo);
    }
}
